defmodule EliPhxPlay.Context.ChatMessages do

  @moduledoc """
  聊天模块
  负责处理聊天相关的逻辑
  """
  import Ecto.Query, warn: false

  alias EliPhxPlay.Repo
  alias EliPhxPlay.ChatSchema.Messages

  @doc """
  获取两个用户之间的私聊消息
  """
  def get_private_messages(user_id, target_id) do
    query = from m in Messages,
      where: (m.sender_id == ^user_id and m.chat_id == ^target_id)
            or (m.sender_id == ^target_id and m.chat_id == ^user_id),
      order_by: [desc: m.inserted_at],
      limit: 50
    Repo.all(query)
  end

end
